/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NannyManager.java
 *
 * Created on Aug 11, 2010, 5:02:42 AM
 */

package GUI;
import config.Database_function;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author QUOCTHGC60050
 */
public class pnf_Manager_Nanny extends javax.swing.JPanel {

    /** Creates new form NannyManager */

    Connection con= Database_function.setConnection();
    Statement st;
    ResultSet re;
    Vector table = new Vector();
    Vector label= new Vector();

    public pnf_Manager_Nanny() {
        initComponents();
        this.jPanelAdvancedSearch.setVisible(false);
        this.setSize(830, 440);
        initTableN();
    }

    public void addTableN() // insert data to table
    {
        try
        {
            Vector temp= new Vector();

            temp.add(re.getString("NannyID"));
            temp.add(re.getString("FirstName"));
            temp.add(re.getString("LastName"));
            temp.add(re.getString("Birthday"));
            temp.add(re.getString("Gender"));
            temp.add(re.getString("Charge"));
            temp.add(re.getString("Experiences"));

            table.add(temp);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void initTableN() // initialze table
    {
        try
        {
            st= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            re=st.executeQuery("select * from tbl_Nanny order by 'NannyID' asc");

            label.add("ID");label.add("FirstName");label.add("LastName");label.add("Birthday");label.add("Gender");label.add("Charge");label.add("Experiences");

           while(re.next())
            {
                addTableN();
            }

            this.jTableNanny.setModel(new DefaultTableModel (table,label));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void SearchN() // invoke search function
    {

        String searchString = jTextFieldSearch.getText();
        String searchType =(String) jComboBoxSearch.getSelectedItem();


        try
        {
            table.removeAllElements();

            if (searchType.equals("NannyID")) // id-based search function
            {
                re=st.executeQuery("select * from tbl_Nanny where NannyID like '%"+searchString+"%' order by NannyID desc");
                while(re.next())
                {
                    addTableN();
                }
            }
            else if (searchType.equals("Name")) // name-based search function
            {
                re=st.executeQuery("select * from tbl_Nanny where (LastName like '"+searchString+"%') or (FirstName like '"+searchString+"%') order by DIFFERENCE (Firstname, '"+searchString+"') desc");
                while(re.next())
                {
                    addTableN();
                }
            }

            this.jTableNanny.setModel(new DefaultTableModel (table,label));

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

    }

    public void advancedSearchN() // invoke advanced search function
    {
        String searchString = jTextFieldSearch.getText();
        String Gender= (String) jComboBoxGender.getSelectedItem();
        String Experiences = jTextFieldExperience.getText();
        String ChargeT = jTextFieldChargeT.getText();
        String ChargeF = jTextFieldChargeF.getText();
        String AgeT = jTextFieldAgeT.getText();
        String AgeF = jTextFieldAgeF.getText();
        String Query="select * from tbl_Nanny";
        table.removeAllElements();
        try
        {
            // define name condition
            Query+=" where ((LastName like '"+searchString+"%') or (FirstName like '"+searchString+"%'))";

            // define experience condition
            if (!Experiences.equals(""))
                Query+=" and (Experiences >="+Experiences+")";

            // define gender condition
            if (!Gender.equals("Both"))
                Query+="and (Gender like '"+Gender+"')";

            // validate and define charge condition
            if (!ChargeT.equals("") && (!ChargeF.equals("")))
            if ((Float.parseFloat(ChargeF) >= Float.parseFloat(ChargeT)))
            {
                JOptionPane.showMessageDialog(this, "Charge input invalid!","ERROR",JOptionPane.ERROR_MESSAGE,new javax.swing.ImageIcon(getClass().getResource("/image/exit.png")));
            }
            else
            {
                
                    Query+=" and (Charge <= "+ChargeT+")";
                if (!ChargeF.equals(""))
                    Query+=" and (Charge >= "+ChargeF+")";
            }

            // validate and define age condtion
            if (!AgeT.equals("") && !AgeF.equals(""))
            if (Integer.parseInt(AgeF) >= Integer.parseInt(AgeT))
            {
                JOptionPane.showMessageDialog(this, "Age input invalid!","ERROR",JOptionPane.ERROR_MESSAGE,new javax.swing.ImageIcon(getClass().getResource("/image/exit.png")));
            }
            else
            {
                if (!AgeT.equals(""))
                    Query+="and (DateDIFF(day,Birthday,getDate()) <="+AgeT+"*365.25)";
                if (!AgeF.equals(""))
                    Query+="and (DateDIFF(day,Birthday,getDate()) >="+AgeF+"*365.25)";
            }
            

            Query+="order by DIFFERENCE (Firstname, '"+searchString+"') desc";

            re=st.executeQuery(Query);
            while (re.next())
            {
                addTableN();
            }

            this.jTableNanny.setModel(new DefaultTableModel (table,label));
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableNanny = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int vColIndex)
            {
                return false;
            }
        }
        ;
        jLabel1 = new javax.swing.JLabel();
        jTextFieldSearch = new javax.swing.JTextField();
        jComboBoxSearch = new javax.swing.JComboBox();
        jButtonSearch = new javax.swing.JButton();
        jCheckBoxAdvancedSearch = new javax.swing.JCheckBox();
        jPanelAdvancedSearch = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldAgeT = new javax.swing.JTextField();
        jTextFieldChargeT = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboBoxGender = new javax.swing.JComboBox();
        jTextFieldExperience = new javax.swing.JTextField();
        jTextFieldAgeF = new javax.swing.JTextField();
        jTextFieldChargeF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();

        setBackground(new java.awt.Color(227, 239, 255));
        setPreferredSize(new java.awt.Dimension(800, 400));

        jTableNanny.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "ID", "First Name", "Last Name", "AgeGroup", "Gender", "Address", "Status"
            }
        ));
        jTableNanny.setFocusable(false);
        jTableNanny.setGridColor(new java.awt.Color(213, 213, 213));
        jTableNanny.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTableNannyMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTableNanny);

        jLabel1.setText("Search: ");

        jComboBoxSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Name", "NannyID" }));

        jButtonSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Search1.png"))); // NOI18N
        jButtonSearch.setText("Search");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jCheckBoxAdvancedSearch.setBackground(new java.awt.Color(227, 239, 255));
        jCheckBoxAdvancedSearch.setText("     Advanced Search");
        jCheckBoxAdvancedSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxAdvancedSearchActionPerformed(evt);
            }
        });

        jPanelAdvancedSearch.setBorder(javax.swing.BorderFactory.createTitledBorder("AdvanceSearch"));
        jPanelAdvancedSearch.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanelAdvancedSearch.setEnabled(false);
        jPanelAdvancedSearch.setFocusable(false);
        jPanelAdvancedSearch.setOpaque(false);
        jPanelAdvancedSearch.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel6.setText("Age ");
        jPanelAdvancedSearch.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 30, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setText("Charge");
        jPanelAdvancedSearch.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 60, -1));
        jPanelAdvancedSearch.add(jTextFieldAgeT, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 20, 130, -1));
        jPanelAdvancedSearch.add(jTextFieldChargeT, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 50, 130, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Gender       :");
        jPanelAdvancedSearch.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 20, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("Experience:");
        jPanelAdvancedSearch.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 50, -1, -1));

        jComboBoxGender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Both", "Male", "Female" }));
        jPanelAdvancedSearch.add(jComboBoxGender, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 20, 210, -1));
        jPanelAdvancedSearch.add(jTextFieldExperience, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 50, 210, -1));
        jPanelAdvancedSearch.add(jTextFieldAgeF, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, 130, -1));
        jPanelAdvancedSearch.add(jTextFieldChargeF, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 50, 130, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 10));
        jLabel2.setText("  To");
        jPanelAdvancedSearch.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 50, -1, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 10));
        jLabel7.setText(":  From");
        jPanelAdvancedSearch.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 20, -1, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 10));
        jLabel8.setText(":  From");
        jPanelAdvancedSearch.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 50, -1, -1));

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 10));
        jLabel9.setText("  To");
        jPanelAdvancedSearch.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 20, -1, -1));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/edit_1.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(63, 63, 63)
                .addComponent(jCheckBoxAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(191, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(212, 212, 212)
                .addComponent(jButtonSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                .addGap(80, 80, 80)
                .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                .addGap(78, 78, 78)
                .addComponent(btnEdit, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelAdvancedSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jCheckBoxAdvancedSearch)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(3, 3, 3)
                            .addComponent(jLabel1))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd)
                    .addComponent(jButtonSearch))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        // TODO add your handling code here:
        if (this.jCheckBoxAdvancedSearch.isSelected())
            advancedSearchN();
        else
            SearchN();
}//GEN-LAST:event_jButtonSearchActionPerformed

    private void jCheckBoxAdvancedSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxAdvancedSearchActionPerformed
        // TODO add your handling code here:
        // select and modify search function
        boolean check = this.jCheckBoxAdvancedSearch.isSelected();
        if (check==true) {
            this.jPanelAdvancedSearch.setVisible(true);
            this.jComboBoxSearch.removeAllItems();
            this.jComboBoxSearch.addItem("Name");
        } else {
            this.jPanelAdvancedSearch.setVisible(false);
            this.jComboBoxSearch.removeAllItems();
            this.jComboBoxSearch.addItem("Name");
            this.jComboBoxSearch.addItem("NannyID");
        }
}//GEN-LAST:event_jCheckBoxAdvancedSearchActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        // invoke add nanny function and refresh table
        AddNewNanny an = new AddNewNanny();
        an.setVisible(true);
        label.removeAllElements();
        table.removeAllElements();
        initTableN();
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        // invoke edit nanny function and refresh table
        int d=this.jTableNanny.getSelectedRow();
        if(d>=0) {
            String idNanny=(String)this.jTableNanny.getValueAt(d, 0);
            EditNanny ed = new EditNanny(idNanny);
            ed.setVisible(true);
        }
        else
        JOptionPane.showMessageDialog(this, "You must choose a nanny to edit!","ERROR",JOptionPane.ERROR_MESSAGE,new javax.swing.ImageIcon(getClass().getResource("/image/Exit.png")));
        label.removeAllElements();
        table.removeAllElements();
        initTableN();
}//GEN-LAST:event_btnEditActionPerformed

    private void jTableNannyMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableNannyMouseReleased
        // TODO add your handling code here:
        // modify select method and invoke view detail function
        int selectedRow =  jTableNanny.getSelectedRow();
        if (evt.getClickCount()==2)
        {
            Integer NannyID = Integer.parseInt((String)jTableNanny.getValueAt(selectedRow, 0));
            jdl_nannyinfo dn= new jdl_nannyinfo(null,true,NannyID);
            dn.setVisible(true);
        }              
    }//GEN-LAST:event_jTableNannyMouseReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JCheckBox jCheckBoxAdvancedSearch;
    private javax.swing.JComboBox jComboBoxGender;
    private javax.swing.JComboBox jComboBoxSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanelAdvancedSearch;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableNanny;
    private javax.swing.JTextField jTextFieldAgeF;
    private javax.swing.JTextField jTextFieldAgeT;
    private javax.swing.JTextField jTextFieldChargeF;
    private javax.swing.JTextField jTextFieldChargeT;
    private javax.swing.JTextField jTextFieldExperience;
    private javax.swing.JTextField jTextFieldSearch;
    // End of variables declaration//GEN-END:variables

}
